package com.ruoyi.project.cms.project.controller;

import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.cms.project.domain.Order;
import com.ruoyi.project.cms.project.domain.Process;
import com.ruoyi.project.cms.project.service.OrderService;
import com.ruoyi.project.system.role.domain.Role;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("cms/project")
public class ProjectController extends BaseController {
    @Autowired
    private OrderService orderService;
    String prefix = "/cms/project";

    @GetMapping("/order")
    public String order() {
        return prefix + "/order";
    }

    @RequestMapping("/list")
    @ResponseBody
    public TableDataInfo list(Order order) {
        startPage();
        List<Order> search = orderService.search();
        return getDataTable(search);
    }

    @GetMapping("/showdetail/{roleId}")
    public String showdetail(@PathVariable("roleId") int roleId, ModelMap mmap) {
        mmap.put("order",orderService.searchOne(roleId));
        System.out.println("编辑"+mmap);
        return prefix + "/showdetail";
    }
    @GetMapping("/addprocess/{roleId}")
    public String addprocess(@PathVariable("roleId") int roleId, ModelMap mmap) {
        mmap.put("order",orderService.searchOne(roleId));
       mmap.put("process",orderService.searchA());
        System.out.println(mmap);
        return prefix + "/addprocess";
    }

    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addprocess(Order order , ModelMap mmap) {
        List<Process> processes = order.getProcesses();
        String staute = order.getProject().getStaute();
        int pid = order.getProject().getPid();
        boolean flag = false;
        try {
            if("0".equals(staute)){
                System.out.println(pid+staute);
                orderService.changeStaute(pid,"1");

            }
            for (Process process :
                    processes) {
                System.out.println(process);
                orderService.addProcess(process);
                flag = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  toAjax(flag);
    }

    @GetMapping("/showprocess/{roleId}")
    public String showprocess(@PathVariable("roleId") int roleId, ModelMap mmap) {
        mmap.put("order",orderService.searchOne(roleId));
        mmap.put("process",orderService.searchA1(roleId));
        System.out.println(mmap);

        return prefix + "/showprocess";
    }

}
